package com.code2roc.xhood.asm;

import com.code2roc.xhood.model.ObsureEnum;
import com.code2roc.xhood.util.ObscureUtil;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class LocalVariableAdapter extends MethodVisitor {
    private List<String> paramterList;
    private String className;

    public LocalVariableAdapter(String className,MethodVisitor methodVisitor) {
        super(Opcodes.ASM9, methodVisitor);
        paramterList = new ArrayList<>();
        this.className = className;
    }

    @Override
    public void visitParameter(String name, int access) {
        paramterList.add(name);
        super.visitParameter(name, access);
    }

    @Override
    public void visitLocalVariable(String name, String descriptor, String signature, Label start, Label end, int index) {
        if (!name.equals("this") && !name.startsWith("$") && !paramterList.contains(name)) {
            String obfuscatedName = ObscureUtil.getObscureNameV2(className,name, ObsureEnum.LocalVariable);
            super.visitLocalVariable(obfuscatedName, descriptor, signature, start, end, index);
        } else {
            super.visitLocalVariable(name, descriptor, signature, start, end, index);
        }
    }
}
